<?php

namespace app\comm\func;

use think\Controller;
use think\Db;

/**
 * 工具包
 * Class CommFunc
 * @package app\comm\func
 */
class CommFunc extends Controller
{
    /**
     * 返回json结果
     * @param $data
     */
    public static function ajaxReturn($data)
    {
        exit(json_encode($data));
    }

    /**
     * 验证request指定字段合法性
     * @param $data
     * @param $arr
     */
    public static function emptyVerify($data, $arr)
    {
        foreach ($arr as $k => $v) {
            if (!isset($data[$v])) {
                CommFunc::ajaxReturn(['status' => 0, 'msg' => "{$v} is not set"]);
            }
            if ($data[$v] === '' || $data[$v] === null) {
                CommFunc::ajaxReturn(['status' => 0, 'msg' => "{$v} is not empty"]);
            }
        }
    }
    public static function is_RollBack()
    {
        Db::rollback();
        CommFunc::ajaxReturn(Consts::$failure);
    }

    /**
     * 存放request指定字段
     * @param $condition
     * @param $request
     * @param array $arr
     */
    public static function conditionAdd(&$condition, $request, $arr = [])
    {
        foreach ($arr as $k => $v) {
            if (isset($request[$v])) {
                $condition[$v] = $request[$v];
            }
        }
    }

    /**
     * 删除指定字段
     * @param $condition
     * @param $delcondition
     * @return mixed
     */
    public static function remove($condition, $delcondition)
    {
        if (!is_array($delcondition)) {
            unset($condition[$delcondition]);
        } else {
            foreach ($delcondition as $k => $v) {
                unset($condition[$v]);
            }
        }
        return $condition;
    }
}
